package com.example.demo;

import jakarta.servlet.http.HttpSession;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public Boolean login(@RequestParam String userName,
                         @RequestParam String password,
                         HttpSession session){
        if(!StringUtils.hasLength(userName) && !StringUtils.hasLength(password)){
            return false;
        }
        if("admin".equals(userName) && "admin".equals(password)){
            //存储一下userName
            session.setAttribute("userName",userName);
            return true;
        }
        return false;
    }


    @RequestMapping(value = "/getLoginUser",method = RequestMethod.GET)
    public String getLoginUser(HttpSession session){
        String userName = (String) session.getAttribute("userName");
        if(userName == null){
            return "找不到用户";
        }
        return "用户名是: " + userName;
    }
}
